package com.project.api.ym.dao;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.project.api.ym.model.Activity;
import com.project.api.ym.model.ActivityAttach;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import java.sql.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Repository
public class ActivityAttachDao {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    @Qualifier("mainJdbcTemplate")
    private JdbcTemplate mainJdbcTemplate;

    public long ActivityAttachCreate(JSONObject json,long activity_id) throws Exception{
        String sql="insert into "+ ActivityAttach.tableName +"(activity_id,files,`content`) " +
                "values(?,?,?)";
        long id=0;
//        try {
            KeyHolder keyHolder = new GeneratedKeyHolder();
            mainJdbcTemplate.update(new PreparedStatementCreator() {
                @Override
                public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
                    PreparedStatement ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
                    ps.setLong(1,activity_id);
                    List files= (ArrayList<String>) json.getOrDefault("files",new ArrayList<>());
                    JSONArray jsonArray=new JSONArray(files);
                    String fileStr=jsonArray.toString();
//                    String fileStr = String.join(",",files);
//                    String[] f = files.toArray(new String[files.size()]);
//                    String fileStr= Arrays.toString(f);
                    ps.setString(2,fileStr);
                    String content = (String) json.getOrDefault("content",null);
                    if (content == null) ps.setNull(3, Types.NULL);else ps.setString(3, content);
                    return ps;
                }
            }, keyHolder);
            id=keyHolder.getKey().intValue();
//        } catch (Exception  e) {
//            logger.error(e.getMessage());
//            return id;
//        }
        return id;
    }

    public long ActivityAttachUpdate(long activity_id,JSONObject json){
        String sql="update "+ActivityAttach.tableName+" set files=? , `content` = ? where activity_id= ?";
        List files= (ArrayList<String>) json.getOrDefault("files",new ArrayList<>());
        JSONArray jsonArray=new JSONArray(files);
        String fileStr=jsonArray.toString();
//        String fileStr = String.join(",",files);
//        String[] f = files.toArray(new String[files.size()]);
//        String fileStr=Arrays.toString(f);
        String content = (String) json.getOrDefault("content",null);
        Object c=(content == null)?Types.NULL:content;
        int res=mainJdbcTemplate.update(sql,fileStr,c,activity_id );
        return res;
    }

    public boolean ActivityAttachDelete(ArrayList<String> activity_ids){
        String i="("+String.join(",", activity_ids)+")";
        String sql="delete from "+ActivityAttach.tableName+" where activity_id in"+i;
        int res=mainJdbcTemplate.update(sql);
        return true;
    }

    public ActivityAttach GetActivityAttach(long activity_id){
        ActivityAttach res=new ActivityAttach();
        String sql = "SELECT * FROM "+ ActivityAttach.tableName +" where activity_id=?";
        try {
            res = (ActivityAttach) mainJdbcTemplate.queryForObject(
                    sql, new Object[] {activity_id},new BeanPropertyRowMapper(ActivityAttach.class));
        } catch (EmptyResultDataAccessException e) {//捕获查询不到
            return res;
        }
        return res;
    }
}
